/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package it.kirys.rilego.engine.loaders.imagesources;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Federico Improta
 */
public class SourceInfo {

	private ImageType type = null;
	private Orientation orientation = null;
	private int width = -1;
	private int height = -1;

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public boolean isKnown() {
		return type != null && width > 0 && height > 0;
	}

	public Orientation getOrientation() {
		return orientation;
	}

	private void analyzeStream(InputStream stream) {
		ImageInfo ii = new ImageInfo();
		ii.setInput(stream); // in can be InputStream or RandomAccessFile
		ii.setDetermineImageNumber(true); // default is false
		ii.setCollectComments(true); // default is false
		if (!ii.check()) {
			System.err.println("Not a supported image file format.");
		} else {
			this.width = ii.getWidth();
			this.height = ii.getHeight();
			this.orientation = Orientation.getFromSize(width, height);
			switch (ii.getFormat()){
				case ImageInfo.FORMAT_JPEG:
					type= ImageType.JPG;
					break;
				case ImageInfo.FORMAT_PNG:
					type= ImageType.PNG;
					break;
			}

		}
	}//private void analyzeStream(InputStream stream)




	public SourceInfo(IImageSource source) {
		try {
			analyzeStream(source.getStream());
		} catch (IOException ex) {
			Logger.getLogger(SourceInfo.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}
